package com.googlecode.gunncs.graph;

import info.monitorenter.gui.chart.Chart2D;
import info.monitorenter.gui.chart.ITrace2D;
import info.monitorenter.gui.chart.rangepolicies.RangePolicyFixedViewport;
import info.monitorenter.gui.chart.rangepolicies.RangePolicyUnbounded;
import info.monitorenter.gui.chart.traces.Trace2DLtd;
import info.monitorenter.util.Range;

import java.awt.Color;

import javax.swing.JFrame;

/**
 * 
 * @author ajc
 * 
 */
public class LinePlot {

	private static final Color LINE_COLOR = Color.gray;
	private static final Color BG_COLOR = Color.BLACK;

	private Chart2D chart;
	private ITrace2D trace;

	public LinePlot(String name, int capacity) {
		chart = new Chart2D();
		trace = new Trace2DLtd(capacity);

		chart.addTrace(trace);

		// swing jframe
		JFrame frame = new JFrame(name);
		frame.getContentPane().add(chart);
		frame.setVisible(true);

		trace.setName(name);
		trace.setColor(LINE_COLOR);
		chart.setBackground(BG_COLOR);
	}

	public void setYRangingFixed(double min, double max) {
		Range r = new Range(min, max);
		chart.getAxisY().setRange(r);
		chart.getAxisY().setRangePolicy(new RangePolicyFixedViewport(r));
	}

	public void setYRangingAuto() {
		chart.getAxisY().setRangePolicy(new RangePolicyUnbounded());
	}

	public void addPoint(double x, double y) {
		trace.addPoint(x, y);
	}

	public static void main(String[] args) {
		LinePlot frame = new LinePlot("sin(x)", 50);
		// frame.setFixedYRange(-1, 1);
		frame.setYRangingAuto();
		double x = 0;
		for (;;) {
			System.out.println(x);
			frame.addPoint(x += .1, Math.sin(x));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}